package my.android.main;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import my.android.message.R;
import my.android.message.Sms;

public class MainActivity extends Activity {

    private List<Sms> listSms;
    private String message = "";
    private TextView view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        view = (TextView)findViewById(R.id.view);
        listSms = getAllSms();
        for(int i = 0; i < listSms.size(); i++){
            message += i+1 + "." + listSms.get(i).get_folderName() + "\n" + listSms.get(i).get_address() + "\n"  +
                    getDate(listSms.get(i).get_time()) + "\n" + listSms.get(i).get_msg() + "\n" + listSms.get(i).get_readState();
        }
        view.setText(message);




        //set time to check sms sent and set count of listSms for first time
        long currentTime = System.currentTimeMillis();
        SharedPreferences.Editor editor = getSharedPreferences("time", MODE_PRIVATE).edit();
        editor.putLong("last_time_checked", currentTime);
        editor.commit();

        PendingIntent outgoingSmsLogger = PendingIntent.getBroadcast(this, 0, new Intent( "CHECK_OUTGOING_SMS"), 0);
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, currentTime + 60000L, 60000L, outgoingSmsLogger);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public List<Sms> getAllSms(){
        List<Sms> lstSms = new ArrayList<Sms>();
        Sms objSms;
        Uri path = Uri.parse("content://sms/");
        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(path, null, null, null, null);
        int totalSms = cursor.getCount();
        if(cursor.moveToFirst()){
            for(int i = 0; i< totalSms; i++){
                objSms = new Sms();
                objSms.set_id(cursor.getString(cursor.getColumnIndexOrThrow("_id")));
                objSms.set_address(cursor.getString(cursor.getColumnIndexOrThrow("address")));
                objSms.set_msg(cursor.getString(cursor.getColumnIndexOrThrow("body")));
                objSms.set_readState(cursor.getString(cursor.getColumnIndexOrThrow("read")));
                objSms.set_time(cursor.getString(cursor.getColumnIndexOrThrow("date")));
                if(cursor.getString(cursor.getColumnIndexOrThrow("type")).contains("1")){
                    objSms.set_folderName("inbox");
                }else {
                    objSms.set_folderName("sent");
                }
                lstSms.add(objSms);
                cursor.moveToNext();
            }
        }
        cursor.close();
        return  lstSms;
    }

    private String getDate(String date){
        return new SimpleDateFormat("dd/MM/yyyy-HH:mm:ss").format(Long.parseLong(date)).toString();
    }
    
}
